package com.wl.api.service.business.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wl.api.common.utils.CollectionUtil;
import com.wl.api.common.utils.PageInfo;
import com.wl.api.common.utils.RWYException;
import com.wl.api.dto.business.CustomerUserDTO;
import com.wl.api.dto.business.CustomerUserGradeDTO;
import com.wl.api.mapper.business.CustomerUserGradeMapper;
import com.wl.api.model.business.CustomerUserGrade;
import com.wl.api.service.business.ICustomerUserGradeService;
import com.wl.api.service.business.ICustomerUserService;
import com.wl.api.service.system.ISecurityGradeService;

import lombok.extern.log4j.Log4j;

/**
 *
 * CustomerUserGrade 表数据服务层接口实现类
 *
 */
@Service
@Log4j
public class CustomerUserGradeServiceImpl extends ServiceImpl<CustomerUserGradeMapper, CustomerUserGrade> implements ICustomerUserGradeService {

	@Autowired
	CustomerUserGradeMapper userGradeMapper;
	
	@Autowired
	ISecurityGradeService gradeService;
	
	@Autowired
	ICustomerUserService customerUser;
	
	@Transactional
	public void insertOrUpdateCustomerGradeBatch(CustomerUserGradeDTO param) throws Exception {
		Assert.isTrue(param.getGradeId() != null);
		if(Objects.isNull(gradeService.selectById(param.getGradeId()))) {
			log.error(" Abnormal  the administrator trying to modify the customer grade... admin user_id :" + param.getCreateUserId() + " gradeId:"+ param.getGradeId());
			throw RWYException.create(403);	
		}
		List<CustomerUserGrade> gradeList = selectList(new EntityWrapper<CustomerUserGrade>().in("cust_id", param.getUserIds()));
		Set<Long> ids = CollectionUtil.isEmpty(gradeList) ? new HashSet<>(): gradeList.parallelStream().map(CustomerUserGrade::getCustId).distinct().collect(Collectors.toSet());
		
		if (!CollectionUtils.isEmpty(gradeList)) {
			updateBatchById(gradeList.parallelStream().map(s -> s.updateMsg(param.getGradeId(), param.getUpdateUserId(), param.getUpdateUser())).collect(Collectors.toList()));
		}
		
		List<CustomerUserGrade> list = param.getUserIds().parallelStream().filter(x -> !ids.contains(x)).map(s -> 
		                     CustomerUserGrade.builder().gradeId(param.getGradeId()).createTime(new Date()).updateUser(param.getUpdateUser()).updateUserId(param.getUpdateUserId())
		                     .custId(s).build()).collect(Collectors.toList());
		if (!CollectionUtils.isEmpty(list)) {
			insertBatch(list);
		}
	}
	
	public PageInfo<CustomerUserDTO> getCustGradePage(CustomerUserGradeDTO param) {
		PageInfo<CustomerUserDTO> pageInfo = new PageInfo<CustomerUserDTO>();
		int count =  userGradeMapper.getCustomerUserGradeByParamCount(param);
		List<CustomerUserDTO> list = userGradeMapper.getCustomerUserGradeByParam(param);
		
		return pageInfo.assemPaInfo(param.getPage(), count, param.getPageSize(), list);
	}
	
}